﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class KSLDeals_KSLDeals : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.HasKeys())
            {
                if (Request.QueryString["listid"] != String.Empty)
                    Session["listid"] = Request.QueryString["listid"];
                else
                    Session["listid"] = 1;

                Session["servername"] = Request.QueryString["servername"];
                Session["projectname"] = Request.QueryString["projectname"];
                Session["campaignname"] = Request.QueryString["campaignname"];
                Session["agentid"] = Request.QueryString["agentid"];


                Session["App"] = new DataAccess("");
                loadNotes();

                LoadCustData();
            }
        }
        else
        {
            if (((DropDownList)DispoDDLSales1.FindControl("ddlDispo")).SelectedIndex > 0) //sale disposition selected
            {
                if (tbTransID.Text.Trim() == "")// no transaction id entered
                {
                    lblSaleError.Text = "Enter TransactionID";
                    DispoDDL1.Visible = true;
                    ((DropDownList)DispoDDLSales1.FindControl("ddlDispo")).SelectedIndex = 0;
                }
                else //transaction id entered and sale disposition selected
                {
                    ((DataAccess)Session["App"]).PutCallData("updatedate", DateTime.Now.ToString());
                    ((DataAccess)Session["App"]).PutCallData("transactionid", tbTransID.Text.Trim());
                    ((DataAccess)Session["App"]).CommitCallData();
                    lblSaleError.Text = "";
                    DispoDDL1.Visible = false;
                }
            }
            else //no sale disposition selected
            {
                tbTransID.Text = "";
                ((DataAccess)Session["App"]).PutCallData("updatedate", DateTime.Now.ToString());
                ((DataAccess)Session["App"]).PutCallData("transactionid", tbTransID.Text.Trim());
                ((DataAccess)Session["App"]).CommitCallData();
                lblSaleError.Text = "";
                DispoDDL1.Visible = true;
            }
        }
    }

    protected void btnAddNote_Click(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(tbAddNote.Text.Trim()))
        {
            var notestring = DateTime.Now.ToString() + " - " + tbAddNote.Text + "<br/>" + ((DataAccess)Session["App"]).GetCallData("notes");

            ((DataAccess)Session["App"]).PutCallData("notes", notestring );
            ((DataAccess)Session["App"]).CommitCallData();

            loadNotes();
        }
        else
        {
            lNoteError.Text = "Cannot insert empty note.";
            lNoteError.ForeColor = System.Drawing.Color.Red;
            tbAddNote.Text = "";
        }

    }

    private void loadNotes()
    {
        lblDisplayNotes.Text = ((DataAccess)Session["App"]).GetCallData("notes");
        tbAddNote.Text = "";
    }

    protected void btnSaveEmail_Click(object sender, System.EventArgs e)
    {
        ((DataAccess)Session["App"]).PutCallData("email", tbEmail.Text);
        ((DataAccess)Session["App"]).PutCallData("Contact_name", tbContactName.Text);
        ((DataAccess)Session["App"]).PutCallData("updatedate", DateTime.Now.ToString());
        if (chkVerified.Checked == true) 
        {
            if (chkVerified.Enabled == true)
            {
                ((DataAccess)Session["App"]).PutCallData("verifieddate", DateTime.Now.ToString());  
                ((DataAccess)Session["App"]).PutCallData("verified", "1");
            }

        }
        if (tbContactPhone.Text != "")
        {
            ((DataAccess)Session["App"]).PutCallData("Contact_Phone", tbContactPhone.Text);
            UpdatePhone(Session["listid"].ToString(), tbContactPhone.Text);
        }

        
        //((DataAccess)Session["App"]).PutCallData("transactionid", tbTransID.Text);
        ((DataAccess)Session["App"]).PutCallData("webusercode", ((DropDownList)DispoDDL1.FindControl("ddlDispo")).SelectedValue);

        ((DataAccess)Session["App"]).CommitCallData();
        lblError.ForeColor = Color.Blue;
        lblError.Text = "Contact Info saved successfully";

        // Disable Checkbox so it can not be unverified
        if (chkVerified.Checked == true)
        {
            if (chkVerified.Enabled == true)
            {
                chkVerified.Enabled = false; 
            }

        }
    }

    public void LoadCustData()
    {
        tbListid.Text = ((DataAccess)Session["App"]).GetList("enterpriselistid");
        tbBusinessname.Text = ((DataAccess)Session["App"]).GetList("businessname");
        tbBusinessPhone.Text = ((DataAccess)Session["App"]).GetList("businessphone");
        tbAddress.Text = ((DataAccess)Session["App"]).GetList("address");
        tbCity.Text = ((DataAccess)Session["App"]).GetList("city");
        tbState.Text = ((DataAccess)Session["App"]).GetList("state");
        tbZip.Text = ((DataAccess)Session["App"]).GetList("zip");
        tbCatName.Text = ((DataAccess)Session["App"]).GetList("categoryname");
        tbThemeName.Text = ((DataAccess)Session["App"]).GetList("themename");
        if (((DataAccess)Session["App"]).GetCallData("email").Equals(""))
        {
            tbEmail.Text = ((DataAccess)Session["App"]).GetList("businessemail");
        }
        else
        {
            tbEmail.Text = ((DataAccess)Session["App"]).GetCallData("email");
        }
        tbContactName.Text = ((DataAccess)Session["App"]).GetCallData("Contact_Name");
        tbContactPhone.Text = ((DataAccess)Session["App"]).GetCallData("Contact_Phone");
        if (((DataAccess)Session["App"]).GetCallData("Verified").Equals("1"))
        {
            chkVerified.Checked = true;
            chkVerified.Enabled = false;
        }
        else
        {
            chkVerified.Checked = false;
        }

    }   
    
    public void UpdatePhone(string tListid, string tPhone)
    {
        SqlConnection SQLConn = new SqlConnection();
        SQLConn.ConnectionString = "Data Source=COGSQL; Initial Catalog=Voicenet_KSLDeals; User ID = portaluser; Password = sqluser";
        SQLConn.Open();
        if (SQLConn.State == System.Data.ConnectionState.Open)
        {
           
            string SQLUpdateText = "EXEC [f_UpdatePhone] " + tListid + ", '" + tPhone + "'" ;
            SqlCommand SqlCmd = new SqlCommand(SQLUpdateText, SQLConn);
            SqlCmd.CommandType = System.Data.CommandType.Text;
            SqlCmd.ExecuteNonQuery();
            //SqlDataAdapter dataAdapterUsercodes = new SqlDataAdapter(SqlCmdUsercodes);
            //dataAdapterUsercodes.Fill(dtUsercodes);
        }

    }

}

